#include "StdAfx.h"

///////////////////////////////
// SMA Initialize

void SMA::Initialize(vector<Quotation> stockQuotations)
{
	double sum=0;
	int i;

	//first value
	for(i=0; i<this->_period-1; i++) {
		sum += stockQuotations[i].Prices[this->_price];
		this->_values.push_back(EMPTY);
	}

	sum += stockQuotations[i].Prices[this->_price];
	this->_values.push_back(sum/(double)this->_period);
	i++;

	double firstValue, lastValue;

	while (i < (int)stockQuotations.size()) 
	{
		firstValue = stockQuotations[i - this->_period].Prices[this->_price] / (double)this->_period;
		lastValue = stockQuotations[i].Prices[this->_price] / (double)this->_period;

		this->_values.push_back( (this->_values.back() + lastValue - firstValue) );
		i++;
	}
}


///////////////////////////////
// EMA Initialize

void EMA::Initialize(vector<Quotation> stockQuotations)
{
	double sum = .0;
	int i;
	
	// initialize first value with a SMA
	for(i=0; i<this->_period-1; i++) {
		sum += stockQuotations[i].Prices[this->_price];
		this->_values.push_back(EMPTY);
	}

	sum += stockQuotations[i++].Prices[this->_price];	
	this->_values.push_back( sum/(double)this->_period );

	double k = (2.0 / (double)(this->_period + 1));
	double exp = 1.0 - k;

	while(i < (int)stockQuotations.size()) {
		this->_values.push_back( (stockQuotations[i].Prices[this->_price] * k) + ( this->_values.back() * exp) );
		i++;
	}

	this->_values[this->_period-1] = EMPTY;
}


///////////////////////////////
// WMA Initialize

void WMA::Initialize(vector<Quotation> stockQuotations)
{
	double sum = .0, denom = (double)(this->_period * (this->_period + 1)) / 2.0;
	int x = 1, i;

	for (i = 0; i < this->_period - 1; i++, x++) {
		sum += stockQuotations[i].Prices[this->_price] * x;
		this->_values.push_back(EMPTY);
	}

	sum += stockQuotations[i].Prices[this->_price] * x;
	this->_values.push_back(sum/denom);
	i++;
	
	int j;
	while( i < (int)stockQuotations.size())
	{
		x=1;
		sum=.0;

		for (j = i - this->_period + 1; j < i + 1; j++, x++) 
			sum += ( stockQuotations[j].Prices[this->_price] * x );

		this->_values.push_back( sum/denom );
		i++;
	}
}

